<?php
namespace app\common\model;

use think\Model;
use think\Db;
class Paper extends Model
{
    // 指定表名,不含前缀
    protected $name = 'paper';

    /**
     * 根据参数获取列表
     * @param array $arg ['p_subject'=>2,'id'=>1....]
     * @param $field 'id,p_subject...'
     */
    public function getListByArg(array $arg=null,$field='*',$argor=1){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->where($argor)->field($field)->select();
        return $arg;
    }
	public function getListByArgOne(array $arg=null,$field='*'){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->field($field)->find();
        return $arg;
    }
	
	public function getPaperType(){
		return array(
					'1'=>'随机抽题',
					'2'=>'固定抽题',
					);
	}
	
	/**
     * 根据参数获取列表
     * @param array $arg ['p_subject'=>2,'id'=>1....]
     * @param $field 'id,p_subject...'
     */
	 public function getInfo($id){

		if(empty($id)) return false; 
		
		$result = $this->where('id',$id)->field('*')->find()->getData();
		return $result;
		
	 }
	 
	/* 更新或者新增函数 */
	public function DataSave($data){
		$id = $data['id'];
        if(empty($id)) $data['admin_reg'] = $_SESSION['think']['reg'];
		!empty($id) ?  $ret = $this->allowField(true)->isUpdate(true)->save($data, ['id' => $id]) : $ret = $this->allowField(true)->isUpdate(false)->save($data);
		
		return $ret;
	}

	/** 获取试卷属于主库还是从库 **/
	public function getQbankType($paperId){
        $info=$this->alias('a')->join('qbank b','a.p_dbs = b.id')->where("a.id='{$paperId}'")->field('parentid')->find();
        if ($info['parentid']==0){
            return 1;
        }else{
            return 2;
        }
    }
}
